package com.jfinal.demo.plugins;

import org.quartz.*;

import java.text.SimpleDateFormat;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

public class QuartzFactory {

    public static SchedulerFactory sf;

    /**
     * 定时开始任务
     *
     * @param startTime
     * @param id
     * @param name
     * @param group
     * @param jobClass
     */
    public static void startJobOnce(String startTime, int id, String name, String group, Class<? extends Job> jobClass) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:ss:mm");

        try {
            Scheduler sched = sf.getScheduler();
            // define the job and tie it to our HelloJob class
            JobDetail job = newJob(jobClass)
                    .withIdentity("job_" + name + "_" + id, "group_" + group + "_" + id)
                    .requestRecovery()
                    .build();
            job.getJobDataMap().put(group + "_" + name, id);
            // 定时执行
            SimpleTrigger trigger = (SimpleTrigger) newTrigger()
                    .withIdentity("trigger_" + name + "_" + id, "group_" + group + "_" + id)
                    .startAt(sdf.parse(startTime))
                    .build();


            sched.scheduleJob(job, trigger);
            sched.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void startJobForeach(int time, int id, String name, String group, Class<? extends Job> jobClass) {
        Scheduler scheduler = null;
        try {
            scheduler = sf.getScheduler();
            JobDetail job = newJob(jobClass)
                    .withIdentity("job_" + name + "_" + id, "group_" + group + "_" + id)
                    .requestRecovery()
                    .build();

            // 创建一个调度规则, 每3s运行一次
            SimpleScheduleBuilder simpleBuilder = SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(time).repeatForever();
            Trigger trigger = TriggerBuilder.newTrigger().withSchedule(simpleBuilder).startNow().build();

            scheduler.scheduleJob(job, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }

    }

    /**
     * 停止任务
     *
     * @param name
     * @param group
     * @param id
     */
    public static void stopJob(String name, String group, int id) {
        try {
            if (sf != null) {
                Scheduler scheduler = sf.getScheduler();
                TriggerKey triggerKey = TriggerKey.triggerKey("trigger_" + name + "_" + id, "group_" + group + "_" + id);
                Trigger trigger = scheduler.getTrigger(triggerKey);
                if (trigger != null) {
                    scheduler.pauseTrigger(triggerKey);
                    scheduler.unscheduleJob(triggerKey);
                    scheduler.deleteJob(trigger.getJobKey());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
